//
//  RBTool.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/5.
//

#import "RBTool.h"
#import "RBGetUserUUID.h"
#import "sys/utsname.h"

@implementation RBTool

+ (NSDictionary *)rb_jsonStrToDict:(NSString *)jsonStr{
    if (jsonStr == nil) {
        return nil;
    }
    NSData *data = [jsonStr mj_JSONData];
    NSError *err = nil;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
    if (err || ![dict isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    return dict;
}
+ (BOOL)rb_isEmptyStr:(NSString *)string{
    if (string == nil) {
        return YES;
    }
    if (string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    id tempStr = string;
    if (tempStr == [NSNull null]) {
        return YES;
    }
    if ([string isEqual:[NSNull null]]) {
        return YES;
    }
    if (string == (NSString *)[NSNull null]) {
        return YES;
    }
    if (string == (id)[NSNull null]) {
        return YES;
    }
    if([string isKindOfClass:[NSString class]] == NO)
    {
        return YES;
    }
    if(string.length == 0)
    {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
        return YES;
    }
    if ([string.lowercaseString isEqualToString:@"(null)"] || [string.lowercaseString isEqualToString:@"null"] || [string.lowercaseString isEqualToString:@"<null>"]){
        return YES;
    }
    return NO;
}
+ (BOOL)rb_isEmptyArray:(NSArray *)array{
    if (!array) {
        return YES;
    }
    if ([array isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if (![array isKindOfClass:[NSArray class]]) {
        return YES;}
    if (!array.count) {return YES;
    }
    if (array == nil) {
        return YES;
    }
    if (array == NULL) {
        return YES;
    }
    return NO;
}
+ (BOOL)rb_isEmptyDictionary:(NSDictionary *)dict{
    if (!dict) {
        return YES;
    }
    if ([dict isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if (![dict isKindOfClass:[NSDictionary class]]) {
        return YES;
    }
    if (!dict.count) {
        return YES;
    }
    if (dict == nil) {
        return YES;
    }
    if (dict == NULL) {
        return YES;
    }
    return NO;
}
+ (NSString *)rb_getMobileType{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    return deviceString;
}
+(NSString *)rb_getuuid{
    NSString *uuid = (NSString *)[RBGetUserUUID rb_load:@"com.xbwh.MaterialXBWHUser"];
    if (uuid) {
        return uuid;
    }
    NSString *uid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    [RBGetUserUUID rb_save:@"com.xbwh.MaterialXBWHUser" data:uid];
    return uid;
}
+ (NSString *)rb_getCurrentVersion{
    return [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
}
+(UIViewController *)rb_getCurrentController{
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    UIViewController* currentViewController = window.rootViewController;
    BOOL startBegin = YES;
    while (startBegin) {
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
        } else if ([currentViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController* navigationController = (UINavigationController* )currentViewController;
            currentViewController = [navigationController.childViewControllers lastObject];
        } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
            UITabBarController* tabBarController = (UITabBarController* )currentViewController;
            currentViewController = tabBarController.selectedViewController;
        } else {
            NSUInteger childViewControllerCount = currentViewController.childViewControllers.count;
            if (childViewControllerCount > 0) {
                currentViewController = currentViewController.childViewControllers.lastObject;
                return currentViewController;
            } else {
                return currentViewController;
            }
        }
    }
    return currentViewController;
}

@end
